namespace SimpleWebServices.Web.UI.Html.Elements
{
    using System;
    using System.Collections.ObjectModel;
    using System.Xml;
    using System.Xml.Serialization;

    [XmlRoot("optgroup")]
    public class OptionGroup : Element, IOption, IAttributeDisabled, IXmlSerializable
    {
        public OptionGroup()
        {
            this.Options = new Collection<Option>();
        }

        public bool Disabled
        {
            get;
            set;
        }

        public string Label
        {
            get;
            set;
        }

        public Collection<Option> Options
        {
            get;
            private set;
        }

        public override IElement Deserialize(string name, string type, string xml)
        {
            IElement result = null;

            if (null == type && "optgroup".Equals(name, StringComparison.OrdinalIgnoreCase))
            {
                result = xml.Deserialize<OptionGroup>();
            }

            return result;
        }

        public override void ReadXml(XmlReader reader)
        {
            base.ReadXml(reader);

            string value = reader.GetAttribute("label");
            if (!string.IsNullOrEmpty(value))
            {
                this.Label = value;
            }

            this.Disabled = "disabled".Equals(reader.GetAttribute("disabled"), StringComparison.OrdinalIgnoreCase);

            if (reader.IsEmptyElement)
            {
                reader.Read();
            }
            else
            {
                string name = reader.Name;
                reader.Read();
                while (true)
                {
                    if (XmlNodeType.None == reader.NodeType)
                    {
                        break;
                    }
                    else if (XmlNodeType.EndElement == reader.NodeType && reader.Name == name)
                    {
                        reader.Read();
                        break;
                    }
                    else if ("option".Equals(reader.Name, StringComparison.OrdinalIgnoreCase))
                    {
                        this.Options.Add(reader.ReadOuterXml().Deserialize<Option>());
                    }
                    else
                    {
                        reader.Read();
                    }
                }
            }
        }

        public override void WriteXml(XmlWriter writer)
        {
            base.WriteXml(writer);

            if (!string.IsNullOrEmpty(this.Label))
            {
                writer.WriteAttributeString("label", this.Label);
            }

            if (this.Disabled)
            {
                writer.WriteAttributeString("disabled", "disabled");
            }

            foreach (Option option in this.Options)
            {
                writer.WriteRaw(option.ToString());
            }
        }
    }
}